package com.wkhyapp.lm.weigit.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;

import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.CategoryAdapter;
import com.wkhyapp.lm.http.vo.Category;

import java.util.List;

/**
 * Created by fang on 2021/10/12.
 */

public class CategoryDialog {

    private Dialog mDialog;
    private List<Category> categories;
    private Context mContext;
    private View dialog_view;
    private CategoryAdapter categoryAdapter;
    private GridView grid_view;
    public CategoryDialog.callBack  mCallBack;
    public CategoryDialog(Context context, List<Category> categories) {
        this.mContext = context;
        this.categories = categories;
        init();
    }

    public void  init(){
        categoryAdapter  = new CategoryAdapter(mContext,categories,R.layout.item_category);
        mDialog = new Dialog(mContext, R.style.custom_dialog);
        dialog_view = LayoutInflater.from(mContext).inflate(R.layout.dialog_category, null);
        grid_view = (GridView)dialog_view.findViewById(R.id.grid_view);
        mDialog.setContentView(dialog_view);
        grid_view.setAdapter(categoryAdapter);
        categoryAdapter.notifyDataSetChanged();
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 居中位置
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        grid_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCallBack.jump(categories.get(position));
                close();
            }
        });
    }

    public void close(){
        mDialog.dismiss();
    }

    public void show(){
        mDialog.show();
    }

    public void setCallBack(CategoryDialog.callBack callBack){
        this.mCallBack = callBack;
    }


    public  interface  callBack{
        void jump(Category item);
    }
}
